var detailVue = new Vue({
	el: "body",
	created: function() {
		init();
		_this = this;
		loadProduct(_this);
	},
	data: {
		productItem: [],
		isClose: true,
		num: 1,
		isLoading: true,
	},
	methods: {
		showhidden: function() {
			this.isClose = false;
		},
		closehidden: function() {
			this.isClose = true;
		},
		addnum: function() {
			this.num++;
		},
		delnum: function() {
			if(this.num <= 1) {
				$.toast("至少保留一件商品", "cancel", function(toast) {
					console.log(toast);
				});
			} else {
				this.num--
			}
		},
		setfav: function() {
			addFav(_this)
		},
		setcart: function() {
			setCart(_this)
		},
		linkcart: function() {
			toCart()
		},
		buynow: function() {
			var pid =[];
			pid.push(_this.productItem[0].attributes.pid);
			var price = _this.productItem[0].attributes.price[0];
			var img =[];
			img.push(_this.productItem[0].attributes.image[0]);
   			payProducts(pid, price, _this.num, img)
		},
		back:function  () {
			location.href="./index.html"
		}
	}

})

function loadProduct(a) {
	var id = getUrlPro("id")
	var query = new AV.Query('Product');
	query.equalTo("pid", id);
	query.find().then(function(products) {
		console.log(products)
		a.productItem = products;
		a.isLoading = false;
		a.$nextTick(function() {
			$(".swiper-container").swiper({
				loop: true,
				autoplay: 3000,
			});
		})
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function addFav(a) {
	currentUser = AV.User.current();
	if(currentUser) {
		var pid = a.productItem[0].attributes.pid //商品id
		var user = currentUser.attributes.username //当前登录用户
		var id = new AV.Query('Fav');
		id.equalTo("pid", pid);
		var username = new AV.Query('Fav');
		username.equalTo("username", user);
		//设置组合查询的条件
		var fav = AV.Query.and(id, username);
		fav.find().then(function(products) {
			if(products.length == 0) {
				favItem(user, pid)
			} else {
				$.toast("已收藏该商品", function() {
					console.log('close');
				});
			}
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
	} else {
		location.href = "login.html?RetUrl=" + getPageUrl()
	}
}

function favItem(a, b) {
	var Product = AV.Object.extend('Fav')
	var product = new Product();
	product.set('username', a);
	product.set('pid', b);
	product.save().then(function() {
		$.toast("收藏成功", function() {
			console.log('close');
		});
	}, function(error) {
		$.toast("收藏失败", "cancel", function(toast) {
			console.log(toast);
		});
	});
}

function setCart(a) {
	currentUser = AV.User.current();
	if(currentUser) {
		var pid = a.productItem[0].attributes.pid //商品id
		var user = currentUser.attributes.username //当前登录用户
		var id = new AV.Query('Cart');
		id.equalTo("pid", pid);
		var username = new AV.Query('Cart');
		username.equalTo("username", user);
		//设置组合查询的条件
		var cart = AV.Query.and(id, username);
		cart.find().then(function(products) {
			if(products.length == 0) {
				addCart(user, pid, a.num)
			} else {
				var query = new AV.Query('Cart');
				query.equalTo("pid", pid);
				query.find().then(function(products) {
					console.log(products)
					var num = products[0].attributes.num //当前购物车里该商品的数量  
					var rid = products[0].id //该商品在购物车里的真实id
					console.log(rid)
					console.log(num)
					//购物车里已经添加过当前商品，只是修改数量
					var todo = AV.Object.createWithoutData('Cart', rid);
					todo.set('num', num + a.num);
					// 保存到云端
					todo.save();
					$.toast("加入成功", function() {
						console.log('close');
					});
				}).catch(function(error) {
					console.log(JSON.stringify(error));
				});
			}
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
	} else {
		location.href = "login.html?RetUrl=" + getPageUrl2()
	}
}

function addCart(a, b, c) {
	var Product = AV.Object.extend('Cart')
	var product = new Product();
	product.set('username', a);
	product.set('pid', b);
	product.set('num', c);
	product.save().then(function() {
		$.toast("加入成功", function() {
			console.log('close');
		});
	}, function(error) {
		alert("failure")
	});
}

//带有location.search，可以返回详情
function toCart() {
	currentUser = AV.User.current();
	if(currentUser) {
		location.href = "cart.html"
	} else {
		location.href = "login.html?RetUrl=" + getPageUrl2()
	}
}

function payProducts(a, b, c, d) {
	currentUser = AV.User.current();
	var Product = AV.Object.extend('Order')
	var product = new Product();
	product.set('username', currentUser.attributes.username);
	product.set('pid', a);
	product.set('price', b);
	product.set('num', c);
	product.set('img', d);
	product.set('type', "paying");
	product.save().then(function() {
		location.href = "buynow.html"
		//		removePayProduct(a)
	}, function(error) {
		alert("failure")
	});
}